/*
 * Copyright @ 2024 weili23
 * com.itheima.mp.service.impl 14:49
 * All right reserved.
 */

package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/4/16 14:49
 * @version: v1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    @Transactional
    public void deductMoney(Long id, Integer money) {
        //1 根据id查询用户
        User user = getById(id);
        //2 用户状态是否正常
        if(user == null || user.getStatus() ==2){
            throw new RuntimeException("用户状态不正常~");
        }
        //3 用户余额是否充足
        if(user.getBalance()< money){
            throw new RuntimeException("用户余额不充足~");
        }
        //4 扣减库存
        //baseMapper.deductMoney(id,money);
        int remainMoney = user.getBalance()-money;
        lambdaUpdate()
                .set(User::getBalance,remainMoney)
                .set(remainMoney ==0,User::getStatus,2)
                .eq(User::getId,id)
                .eq(User::getBalance,user.getBalance())  //乐观锁
                .update();
    }

    /**
     * @desc lambda查询的方式
     * @author: weili23
     * @params
     * @Date: 2024/4/16 17:49
     * @version: 1.0
     */
    @Override
    public List<User> queryUserbyCondition(UserQuery query) {
        List<User> userList = lambdaQuery()
                .like(query.getName() != null, User::getUsername, query.getName())
                .eq(query.getStatus() != null, User::getStatus, query.getStatus())
                .ge(query.getMinBalance() != null, User::getBalance, query.getMinBalance())
                .le(query.getMaxBalance() != null, User::getBalance, query.getMaxBalance())
                .list();
        return userList;
    }

    @Override
    public UserVO queryUserAndAddrById(Long id) {
        //1 查询用户
        User user = getById(id);
        if (user == null || user.getStatus() ==2) {
            throw new RuntimeException("用户异常~");
        }
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        //根据用户id查询地址信息
        List<Address> list = Db.lambdaQuery(Address.class).eq(Address::getUserId, id).list();
        if(CollectionUtil.isNotEmpty(list)){
            List<AddressVO> addressVOS = BeanUtil.copyToList(list, AddressVO.class);
            userVO.setAddress(addressVOS);
        }
        return userVO;
    }

    @Override
    public PageDTO<User> queryUserPagesByCondition(UserQuery query) {
        Page<User> page = Page.of(query.getPageNo(),query.getPageSize());
        if(StringUtils.isNotEmpty(query.getSortBy())){
            page.addOrder(new OrderItem(query.getSortBy(),query.getIsAsc()));
        }
        Page<User> userPage = lambdaQuery().like(query.getName() != null, User::getUsername, query.getName())
                .eq(query.getStatus() != null, User::getStatus, query.getStatus())
                .page(page);

        PageDTO<User> pageDto = new PageDTO<>();
        pageDto.setPages(userPage.getPages());
        pageDto.setTotal(userPage.getTotal());
        pageDto.setList(userPage.getRecords());
        return pageDto;
    }
}
